package adapterLayer;

import java.awt.Point;
import java.awt.event.KeyEvent;

import gameElements.GController;
import gameLogic.ElementHandler;
import gameView.GameRenderer;
import gameView.ViewHandler;

/**
 * Adapter
 * This is a interpreter between graphics and game model 
 *
 * Fredrik Wallenius and Peter Steiner 
 * DP08 - KTH - 2009
 *
 */
 public class Adapter {

	private GameRenderer gameR;
	private ElementHandler elementH;
	private GController c;
	private ViewHandler vh;
	private String topListName[];
	private int topListTime[];
	
	public Adapter(){
		topListName = new String[5];
		topListTime = new int[5];
	}
	
	/**
	 * Sets the name and time of one high score holder for the given track.
	 * 
	 * @param index
	 * @param name
	 * @param time
	 */
	public void setTopList(int index, String name, int time){
		try{
			topListName[index] = name;
			//System.out.println(name);
			topListTime[index] = time;
		} catch(Exception e){
			// Troligen indexoutofbounds..
		}
	}
	
	/**
	 * Gets the name of a high score holder.
	 * 
	 * @param index	The position on the list.
	 * @return The name.
	 */
	public String getTopListName(int index){
		return topListName[index];
	}
	
	/**
	 * Gets the time of a high score holder.
	 * 
	 * @param index The position on the list.
	 * @return The time.
	 */
	public int getTopListTime(int index){
		return topListTime[index];
	}
	
	/**
	 * Set graphics JFrame ViewHandler (This is graphical interface main window)
	 * @param vh_
	 */
	public void setViewHandler(ViewHandler vh_){
		this.vh = vh_;
	}
	
	/**
	 * Set controller from gameLogic
	 * @param GController
	 */
	public void setController(GController c_){
		this.c = c_;
	}
	
	/**
	 * Get Game model ElementHandler 
	 * @return ElementHandler
	 */
	public ElementHandler getElementHandler(){
		return this.elementH;		
	}
	
	/**
	 * Get graphics GameRenderer (JPanel that paints and shows the game main ingredients)
	 * @return GameRenderer
	 */
	public GameRenderer getGameRenderer(){
		return this.gameR;
	}
	
	/**
	 * Get Game model controller
	 * @return GController
	 */
	public GController getController(){
		return this.c;
	}
	
	/**
	 * Set ElementHandler from gameLogic
	 * @param elementH_
	 */
	public void setElementHandler(ElementHandler elementH_){
		this.elementH = elementH_;		
	}
	
	/**
	 * Set GameRenderer from gameView
	 * @param Gamerenderer
	 */
	public void setGameRenderer(GameRenderer gameR_){
		this.gameR = gameR_;
	}
	
	/**
	 * Will do a call to the current renderer(s) that a screen update is needed.
	 */
	public void updateScreen(){
		vh.update();	
	}
	
	/**
	 * Pass param Point to controller for new possition
	 * @param Point
	 */
	public void mouseControll(Point p_){
		c.setControllerPossitionWithMouse(p_);
	}
	
	/**
	 * Run controller method start
	 * @param Keyevent
	 */
	public void keyPressed(KeyEvent ke){
		c.keyControllMove(ke);
	}
	
	/**
	 * Run controller method stop
	 * @param Keyevent
	 */
	public void keyReleased(KeyEvent ke){
		c.keyControllStop(ke);
	}
	
	/**
	 * not used
	 */
	public void mouseClicked(){
		
		//Pause game!!??
	}
	
	/**
	 * Paus game threads.
	 */
	public void toggleOnOff(){
		this.elementH.toggleOnOff();
	}
	
}
